package hn.cch.grammar.java;


import org.junit.jupiter.api.Test;

import java.util.StringJoiner;

/**
 * 字符串工具类
 */

public class StringDemo {

    @Test
    public void test01() {
        // 32-127是ASCII打印字符：32是空格｜127是删除
        for (int i = 32; i < 128; i++) {
            System.out.printf("%d\t!%c~\n", i, i);
        }
    }


    @Test
    public void test02() {

        // StringBuilder非线程安全类
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("append");
        System.out.println();

        StringBuffer stringBuffer = new StringBuffer();
        // 使用关键字synchronized修饰来保证线程安全
        stringBuffer.append("append");
        stringBuffer.insert(0, "insert");

        String.format("%s, %d, %f", "adf", 12, 123.56);


    }

    @Test
    public void test03() {
        // jdk >= 1.8

        String[] stringArray = {"red", "yellow", "blue", "green", "black", "white"};
        // 分隔符，前缀，后缀:StringJoiner(delimiter[, prefix, suffix])
        StringJoiner stringJoiner = new StringJoiner(",", "{", "}");
        // stringJoiner.setEmptyValue("EmptyValue");
        for (String string : stringArray) {
            stringJoiner.add(string);
        }
        System.out.println(stringJoiner.toString());

        String stringJoin = String.join("|", stringArray);
        System.out.println(stringJoin);

        // 分割字符串分隔符 $ ^ * + | \ . ? ( ) [ ] { } 转义字符\\
        stringArray = stringJoin.split("\\|");




    }
}
